package routes

import (
	"todo_list/api"
	"todo_list/middieware"

	"github.com/gin-contrib/sessions"
	"github.com/gin-contrib/sessions/cookie"
	"github.com/gin-gonic/gin"
)

func NewRouter() *gin.Engine {
	r := gin.Default() // 创建gin引擎
	store := cookie.NewStore([]byte("something-very-secret"))
	//  初始化cookie会话存储
	r.Use(sessions.Sessions("mysession", store))
	//  设置会话中间件
	v1 := r.Group("api/v1") // 定义一个路由组v1
	{
		// 用户操作,在路由组内定义路由
		v1.POST("user/register", api.UserRegister)
		v1.POST("user/login", api.UserLogin)
		authed := v1.Group("/")
		authed.Use(middieware.JWT())
		// 运行时先验证middieware.JWT()这个中间件看有没有这个权限
		{
			authed.POST("task", api.CreateTask)
			authed.GET("task/:id", api.ShowTask)
			authed.GET("task", api.ListTask)
			authed.PUT("task/:id", api.UpdateTask)
			authed.POST("search", api.SearchTask)
			authed.DELETE("task/:id", api.DeleteTask)
		}
	}
	return r
}

/*当应用启动时，可以使用这个引擎来处理HTTP请求*/
